Calculated = function (string) {
	"use strict";
	var seed = {};
	seed.type = 'calculated';
	seed.id = getUniqueId();
	
	seed.genXML = function () {
		var self = this;
		var temp_string = "";
		
		//write Calculated header
		temp_string += "\t<question type=\"calculated\">\n";
		
		//write Category
		//temp_string += "\t<question type=\"category\">\n\t\t<category><text>$course$/" + self.category + "</text>\n\t</category>\n</question>";
		
		//write Name
		temp_string += "\n\t\t<name>\n\t\t\t<text> <![CDATA[<p>" + self.name + "</p>]]></text>\n\t\t</name>\n";
		
		//write Text
		temp_string += "\t\t<questiontext format =\"html\">\n\t\t";
		temp_string += "<text><![CDATA[<p>" + self.qtext + "</p>]]></text>\n\t\t</questiontext>\n\t\t<generalfeedback><text><![CDATA[<p>" + self.feedback + "</p>]]></text></generalfeedback>\n";
		temp_string += "<penalty> 0 </penalty>\n<hidden> 0 </hidden>\n<syncronize> 0 </syncronize>\n<single> 0 </single>\n<answernumbering> abc </answernumbering>\n";
		temp_string += "<shuffleanswers> 1 </shuffleanswers>"
		
		//write answer
		temp_string += "<answer fraction=\"100\">\n\t<text>" + self.atext + "</text>\n\t\t<tolerance> " + self.tolerance + " </tolerance>\n\t\t<tolerancetype> 1 </tolerancetype>\n";
		temp_string += "\t\t<correctAnswerFormat> " + self.correctAnswerFormat + " </correctAnswerFormat>\n\t\t<correctAnswerLength> " + self.correctAnswerLength + " </correctAnswerLength>\n</answer>\n";
		
		//write unit grading
		temp_string += "\t\t<unitgradingtype> 0 </unitgradingtype>\n\t\t<unitpenalty> 0.1 </unitpenalty>\n\t\t<showunits> 3 </showunits>\n\t\t<unitsleft> 1 </unitsleft>\n";
		
		//write dataset-repeat for each variable
		temp_string += "\n\t<dataset_definitions>\n\t\t";
		var count = 1;
		for(var i = self.min; i < self.max; i++)
		{
			temp_string +="dataset_definition>\n\t\t\t<status>\n\t\t\t\t<text> private </text>\n\t\t\t</status>\n\t\t\t<name>\n\t\t\t\t<text>" + self.varName + "</text>\n";
			temp_string += "\t\t\t</name>\n\t\t\t<type>calculated</type>\n\t\t\t<distribution>\n\t\t\t\t<text> uniform </text>\n\t\t\t</distribution>\n\t\t\t<minimum>\n\t\t\t\t<text>";
			temp_string += self.varMin + "</text>\n\t\t\t</minimum>\n\t\t\t<maximum>\n\t\t\t\t<text>" + self.varMax + "</text>\n\t\t\t</maximum>\n\t\t\t<decimals>\n\t\t\t\t<text>" + self.varDec + "</text>\n\t\t\t</decimals>\n\t\t\t<itemcount>";
			temp_string += "1</itemcount>\n\t\t\t<dataset_items>\n\t\t\t\t<dataset_item>\n\t\t\t\t\t<number>";
			temp_string += count + "</number>\n\t\t\t\t\t<value>" + i + "</value>\n\t\t\t\t</dataset_item>\n\t\t\t</dataset_items>\n\t\t<number_of_items>";
			temp_string += " 1</number_of_items>\n\t\t\t</dataset_definition>\n"
		}
		temp_string += "</dataset_definitions>\n</question>";
		
		return temp_string;
	};
	
	seed.genGift = function () {
		return "";
	};
	
	seed.genQSD = function () {
		var self = this;
		var qsdString = "";
		
		qsdString += "CALC:\r\n";
		qsdString += "" + self.name + "; " + self.qtext + "; " + self.atext + "; " + self.tolerance + "; " + self.feedback + "; ";
		qsdString += "" + self.correctAnswerFormat + "; " + self.correctAnswerLength + "; ";
		qsdString += "" + self.varName + "; " + self.varMin + "; " + self.varMax + "; " + self.varDec;
		
		return qsdString;
	};
	
	seed.genPreviewHtml = function () {
		var self = this;
		return template('mc-bank-tmpl', self);
	};
	
	seed.set = function (options) {
		var self = this;
		$.each(options, function (key, value) {
			if (key === 'name') { //name
				self.name = value;
			} else if (key === 'qtext') { //question text has var
				self.qtext = value;
			} else if (key === 'feedback') { //feedback
				self.feedback = value;
			}else if (key === 'tolerance') { //tolerance
				self.tolerance = value;
			} else if (key === 'atext') { //anwer formula respect to var
				self.atext = value;
			}else if (key === 'correctAnswerFormat') {//{decimals = 1} {sigfigs = 2}
				self.correctAnswerFormat = value;
			}else if (key === 'correctAnswerLength') { //answer length 0-9
				self.correctAnswerLength = value;
			}else if (key === 'varMin') { //min
				self.varMin = value;
			}else if (key === 'varName') { //name ex: a, b, c
				self.varName = value;
			}else if (key === 'varMax') { //max
				self.varMax = value;
			}else if (key === 'varDec') { //# of places
				self.varDec = value;
			}
		});
	};
	
	seed.validate = function () {
		var self = this;
		var error = ""
		if(self.name === "") {
			error += "Missing field(s)";
		} else if(self.qtext === "") {
			error += "Missing field(s)";
		} else if(self.atext === "") {
			error += "Missing field(s)";
		} else if(self.varName === "") {
			error += "Missing field(s)";
		} else if(self.varDec === "") {
			error += "Missing field(s)";
		} else if(self.varMin === "") {
			error += "Missing field(s)";
		} else if(self.varMax === "") {
			error += "Missing field(s)";
		}else if(self.tolerance === "") {
			error += "Missing field(s)";
		}else if(self.correctAnswerLength === "") {
			error += "Missing field(s)";
		}
		
		return error;
	};
	
	if (string) {
	// spilt the string up by line
		var self = seed,
			lines = string.split("\r\n"),
			currentLine = 2;
			
		if (lines.length === 1) {
			lines = string.split("\n");
		}
		
		// set up the options object
		var obj = {};
		obj.name = "";
		obj.qtext = "";
		obj.atext = "";
		obj.feedback = "";
		obj.varName = "";
		obj.varMin = "";
		obj.varMax = "";
		obj.tolerance = "";
		obj.decimal = "";
		obj.answerLength = "";
		obj.category = "";
		//console.log(lines);
		
		var line = lines[1];
		line = line.replace("\\;", ";\\");
		var data = line.split(/;(?!\\)/);
		$.each(data, function(index) {
			data[index] = data[index].replace(";\\", "\\;");
		});
		// go through the lines with array data
		// trim is to get rid of leading and trailing whitespace
		obj.name = data[0].trim();
		obj.qtext = data[1].trim();
		obj.atext = data[2].trim();
		obj.tolerance = data[3].trim();
		obj.feedback = data[4].trim();
		obj.correctAnswerFormat = data[5].trim();
		obj.correctAnswerLength = data[6].trim();
		obj.varName = data[7].trim();
		obj.varMin = data[8].trim();
		obj.varMax = data[9].trim();
		obj.varDec = data[10].trim();
		
		seed.set(obj);
	
	};
	
	return seed;
};
